﻿Public Class Form3
    Public nodeRootPath As String = ""
    Public oldRootPath As String = ""
    Public setName As String = ""
    Public oldSetName As String = ""
    Private 参照集 As New Dictionary(Of String, HarryNode)
    Private 另索引 As New Dictionary(Of String, String)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If 另索引.ContainsKey(候选集.SelectedItem) Then
            With Form1
                .新增节点(nodeRootPath, 参照集(另索引(候选集.SelectedItem)).nodeName, 参照集(另索引(候选集.SelectedItem)).ToJson)
                .UpdataNodeTreeView(True)
            End With
            提示.BackColor = Color.Green
            提示.Show("添加成功！", 候选集)
        Else
            提示.BackColor = Color.DarkRed
            提示.Show("添加失败！", 候选集)
        End If
    End Sub

    Private Sub Form3_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged
        If Visible Then
            Text = "当前根路径：" & nodeRootPath & " - 来自：" & setName
            If nodeRootPath <> oldRootPath Or setName <> oldSetName Then
                oldRootPath = nodeRootPath
                oldSetName = setName
                参照集 = 参照.GetPath(setName, nodeRootPath)
                Updata()
            End If
        End If
    End Sub

    Private Sub Updata()
        候选集.Items.Clear()
        另索引.Clear()
        Dim searchText As String = String.Format("*{0}*", TextBox2.Text)
        For i = 0 To 参照集.Keys.Count - 1
            Dim key = 参照集.Keys(i) & interpreter.Search(参照集.Keys(i))
            If key Like searchText Then
                候选集.Items.Add(key)
                另索引.Add(key, 参照集.Keys(i))
            End If
        Next
    End Sub

    Private Sub 候选集_SelectedIndexChanged(sender As Object, e As EventArgs) Handles 候选集.SelectedIndexChanged
        If 另索引.ContainsKey(候选集.SelectedItem) Then
            TextBox1.Text = 参照集(另索引(候选集.SelectedItem)).ToJson
        End If
    End Sub

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        Updata()
    End Sub

    Private Sub 候选集_DoubleClick(sender As Object, e As EventArgs) Handles 候选集.DoubleClick
        Button1_Click(Nothing, Nothing)
    End Sub

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form3_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Hide()
        End If
    End Sub
End Class